import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:interview/components/app/app_prompt_overlay.dart';
import 'package:interview/components/page/page_load_overlay.dart';
import 'package:interview/components/page/page_nav_bar.dart';
import 'package:interview/components/status_bar_warpper.dart';
import 'package:interview/controller/navigation_controller.dart';
import 'package:interview/pages/account/components/input_box.dart';
import 'package:interview/pages/account/components/login_ways.dart';
import 'package:interview/providers/app_refresh_providers.dart';
import 'package:interview/servers/login.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:interview/utils/logger.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});
  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  final navigation = Get.find<NavigationController>();

  String user_name = 'hmheima' ;
  String password = 'Hmheima%123' ;
  bool is_agree = true ;

  void update_user_name(String value){
    setState(() {
      user_name = value;
    });
  }

  void update_password(String value){
    setState(() {
      password = value;
    });
  }

  void update_is_agree(){
    setState(() {
      is_agree = !is_agree;
    });
  }

  void onLogin() async {
    final params = LoginParams(username: user_name, password: password);
    final progress = ValueNotifier<double>(0.0);
    try {
      PageLoadOverlay.show(context: context);
      final isSuccess = await fetchLogin(params);
      if(isSuccess){
        progress.dispose();
        PageLoadOverlay.hide();
        AppPromptOverlay.show(
          context: context, 
          message: "登录成功",
          durationMilliseconds: 700
        );
        Provider.of<AppRefreshProvider>(context, listen: false).triggerRefresh();
        // 延迟执行 triggerRefresh 以避免在 build 阶段调用
        await Future.delayed(Duration(milliseconds: 1000));
        navigation.back();
      }
    } catch (e ,stackTrace ) {
      Logger.error("登录失败: $e", tag: 'onLogin' , stackTrace:stackTrace );
    }
  }


  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).extension<AppColors>()!;
    return StatusBarWrapper(
      statusBarColor: theme.subjectPure!,
      statusBarIconBrightness: Theme.of(context).brightness == Brightness.dark ? Brightness.light : Brightness.dark,
      child: Scaffold(
        appBar: null,
        body: SafeArea(
          child: SingleChildScrollView(
            child: Container(
              padding: const EdgeInsets.only(left: 16 ,right: 16 ,bottom: 16),
              color: theme.subjectPure,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  PageNavBar(
                    theme: theme, 
                    showTitle: true, 
                    title: "登录", 
                    showRight: false
                  ),
                  SizedBox(height: 50,),
                  InputBox(
                    theme:theme,
                    user_name:user_name,
                    password:password,
                    is_agree:is_agree,
                    update_user_name:update_user_name,
                    update_password:update_password,
                    update_is_agree:update_is_agree,
                    onLogin:onLogin
                  ),
                  SizedBox(height: 50,),
                  LoginWays(theme: theme,)
                ],
              ),
            ),
          )
        ),
      ),
    );
  }
}
